package reggie.ruiji.service.impl;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import reggie.ruiji.Dto.DishDto;
import reggie.ruiji.mapper.CategoryMapper;
import reggie.ruiji.mapper.DishMapper;
import reggie.ruiji.mapper.DishflavorMapper;
import reggie.ruiji.pojo.Dish;
import reggie.ruiji.pojo.DishFlavor;
import reggie.ruiji.pojo.Employee;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.service.DishFlavorService;
import reggie.ruiji.service.DishService;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishflavorMapper dishflavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;




    @Override
    public Long count(Long id) {
        return dishMapper.countByid(id);
    }

    @Override
    public void insert(DishDto dishDto) {

        dishMapper.insert(dishDto);
    }

    @Override
    public Long getid(String name) {
        Long id = dishMapper.getid(name);
        return id;
    }

    @Override
    public PageBean pagee(Integer pageNo, Integer pageSize, String name) {

        List<DishDto> emplist = dishMapper.list(name);
        for (DishDto dishDto : emplist) {
            String categoryname = categoryMapper.getNameById(dishDto.getCategoryId());
            dishDto.setCategoryName(categoryname);
        }

        PageBean pageBean = new PageBean(emplist, emplist.size(), pageSize, pageNo);
        return pageBean;
    }

    @Override
    public DishDto getDishDtoById(Long id) {
        Dish dish = dishMapper.getById(id);
        DishDto dishDto = new DishDto();
        List<DishFlavor> dishFlavors = dishflavorMapper.getDishFlavorById(id);
        String name = categoryMapper.getNameById(dish.getCategoryId());
        dishDto.setCategoryName(name);
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Transactional
    @Override
    public void update(DishDto dishDto, HttpServletRequest request) {
        dishDto.setUpdateTime(LocalDateTime.now());
        Long empID=(Long)request.getSession().getAttribute("employee");
        Long dishID=dishDto.getId();
        dishDto.setUpdateUser(empID);
        List<DishFlavor> list=dishDto.getFlavors();

        for(DishFlavor dishFlavor:list){
            dishFlavor.setDishId(dishID);
            dishFlavor.setUpdateUser(empID);
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateUser(empID);
            dishFlavor.setIsDeleted(0);
        }
        dishflavorMapper.deletebydishID(dishID);
        dishflavorMapper.save(list);

        dishMapper.update(dishDto);

    }

    @Override
    public void delete(List<Long> ids) {
        dishMapper.delete(ids);
        dishflavorMapper.delete(ids);
    }

    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        dishMapper.updateStatus(ids, status);
    }

    @Override
    public List<DishDto> getDishByCategoryId(Dish dish) {
        Integer  status = 1;//只查询起售状态下的菜品

        List<DishDto> list =dishMapper.getDishByCategoryId(dish.getCategoryId(),status);
        for (DishDto dishDto :list) {
            String categoryname = categoryMapper.getNameById(dishDto.getCategoryId());
            dishDto.setCategoryName(categoryname);
            List<DishFlavor> dishFlavors = dishflavorMapper.getDishFlavorById(dishDto.getId());
            dishDto.setFlavors(dishFlavors);
        }
        return list;
    }




}